/*! ******************************************************************************
 *
 * Created on 2022-08-01
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.yzbdl.lanius.core.dialog;

import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleMissingPluginsException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.di.ui.spoon.TabItemInterface;
import org.pentaho.di.ui.spoon.job.JobGraph;
import org.pentaho.di.ui.spoon.trans.TransGraph;

/**
 * @author yzbdl@yzbdl.ac.cn
 * @version: 1.0
 * @Date 2020/11/5 18:53
 **/
public class PanelUtils {

    //下载或者卸载插件后更新面板的方法
    public static void updatePanel(TabItemInterface activeTabitem) throws KettleXMLException, KettleMissingPluginsException {

            if (activeTabitem instanceof TransGraph) {
                TransGraph tab = (TransGraph) activeTabitem;
                TransMeta transMeta = tab.getTransMeta();
                TransMeta newMeta = null;
                if(Spoon.getInstance().getRepository() != null) {
                    try {
                        newMeta = Spoon.getInstance().getRepository().loadTransformation(transMeta.getObjectId(),null);
                    } catch (KettleException e) {
                        e.printStackTrace();
                    }
                }
                else {
                    newMeta = new TransMeta(transMeta.getFilename());
                }
                newMeta.clearChanged();
                tab.setTransMeta(newMeta);
                Spoon.getInstance().getActiveTransGraph().setTransMeta(newMeta);
                Spoon.getInstance().refreshGraph();
            } else if (activeTabitem instanceof JobGraph) {
                JobGraph jobGraph = (JobGraph) activeTabitem;
                JobMeta jobMeta = jobGraph.getJobMeta();
                JobMeta jobMetaInfo = null;
                if(Spoon.getInstance().getRepository() != null) {
                    try {
                        jobMetaInfo = Spoon.getInstance().getRepository().loadJob( jobMeta.getObjectId(), null );
                    } catch (KettleException e) {
                        e.printStackTrace();
                    }
                }
                else {
                    jobMetaInfo = new JobMeta(jobMeta.getFilename(), null);
                }
                jobMetaInfo.clearChanged();
                Spoon.getInstance().getActiveJobGraph().setJobMeta(jobMetaInfo);
                jobGraph.setJobMeta(jobMetaInfo);
                Spoon.getInstance().refreshGraph();
            }
    }

}
